<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/3/18
 * Time: 15:49
 */

namespace app\admin\controller;


use think\Db;
use think\Request;
use app\admin\model\Area as AreaModel;

class Broker extends Base
{
    /**
     * @return mixed|\think\response\Json
     * 经纪人展示
     */
    public function index(){
        if(Request::instance()->isAjax()){
            $params = input('param.');
            $where['user_type'] = 2;
            $where['user_del'] = 1;
            //用户名||手机号
            if(isset($params['key']) && $params['key'] != ''){
                $where['user_username|user_phone'] = ['like', '%' . $params['key'] . '%'];
            }
            if(isset($params['sheng']) && $params['sheng'] != ''){
                $where['user_sheng'] = $params['sheng'];
            }
            if(isset($params['shi']) && $params['shi'] != ''){
                $where['user_shi'] = $params['shi'];
            }
            if(isset($params['qu']) && $params['qu'] != ''){
                $where['user_qu'] = $params['qu'];
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')->alias('u')
                ->join('up_area a1', 'u.user_sheng = a1.district_id', 'left')
                ->join('up_area a2', 'u.user_shi = a2.district_id', 'left')
                ->join('up_area a3', 'u.user_qu = a3.district_id', 'left')
                ->field('user_image, user_username, user_id, user_shop, user_phone, user_zheng, 
                user_fan, user_card, user_zhizhao,a1.district sheng,a2.district shi,a3.district qu')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('user_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    /**
     * @return mixed|\think\response\Json
     * 设计师展示
     */
    public function designs(){
        if(Request::instance()->isAjax()){
            $params = input('param.');
            $where['user_type'] = 3;
            $where['user_del'] = 1;
            //用户名||手机号
            if(isset($params['key']) && $params['key'] != ''){
                $where['user_username|user_phone|user_shop'] = ['like', '%' . $params['key'] . '%'];
            }
            if(isset($params['comid']) && $params['comid'] != ''){
                $where['user_company_id_3'] = $params['comid'];
            }
            if(isset($params['sheng']) && $params['sheng'] != ''){
                $where['user_sheng'] = $params['sheng'];
            }
            if(isset($params['shi']) && $params['shi'] != ''){
                $where['user_shi'] = $params['shi'];
            }
            if(isset($params['qu']) && $params['qu'] != ''){
                $where['user_qu'] = $params['qu'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('u_user')->alias('u')
                ->join('up_area a1', 'u.user_sheng = a1.district_id', 'left')
                ->join('up_area a2', 'u.user_shi = a2.district_id', 'left')
                ->join('up_area a3', 'u.user_qu = a3.district_id', 'left')
                ->field('user_image, user_username, user_id, user_shop, user_phone, user_zheng, 
                user_fan, user_card, user_zhizhao,a1.district sheng,a2.district shi,a3.district qu')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('user_id desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $company = Db::table('up_company')
            ->field('id, name')
            ->select();
        $this->assign('company',$company);
        // 省
        $province = Db::table('up_area')->where('level', 1)->field('district_id, district')->select();
        $this->assign([
            'province' => $province
        ]);
        return $this->fetch();
    }
    /**
     * 设计师案例
     * @return mixed|\think\response\Json
     */
    public function cases(){
        if(Request::instance()->isAjax()){
            $user_name = input('user_name');
            $where['cc_u_id'] = input('user_id');
            //用户名||手机号
            if(isset($user_name) && $user_name != ''){
                $where['cc_title|cc_name|cc_style'] = ['like', '%' . $user_name . '%'];
            }

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('company_case')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('company_case')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('cc_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    if($v['cc_image']){
                        $lists[$k]['cc_image'] = explode(',',$v['cc_image']);
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user_id = input('user_id');
        $this->assign('user_id',$user_id);
        return $this->fetch('broker/cases');
    }
    /**
     * @return mixed|\think\response\Json
     * 添加经纪人
     */
    public function brokerAdd(){
        if(Request::instance()->isPost()){
            $params = input('param.');
            
            $user_name = trim($params['user_name'], ' ');
            $user_name_len = mb_strlen($user_name, 'UTF8');
            if($user_name_len > 15){
                return json(['code' => 100, 'msg' => '用户名不超过15个字符']);
            }
            
            $user_shop = trim($params['user_shop'], ' ');
            $user_shop_len = mb_strlen($user_shop, 'UTF8');
            if($user_shop_len > 100){
                return json(['code' => 100, 'msg' => '门店信息不超过100个字符']);
            }
            $have = Db::table('up_u_user')->where('user_phone',$params['user_phone'])->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有此手机号码！']);
            }
            $data = [
                'user_username' => $user_name,//用户名
                'user_shop' => $user_shop,//门店信息
                'user_phone' => $params['user_phone'],//联系方式
                'user_time' => date('Y-m-d H:i:s'),//注册时间
                'user_status' => 1,//状态
                'user_reject_time' => date('Y-m-d H:i:s'),//审核通过时间
                'user_zheng' => $params['user_zheng'],//身份证正面
                'user_fan' => $params['user_fan'],//身份证反面
                'user_card' => $params['user_card'],//名片
                'user_zhizhao' => $params['user_zhizhao'],//营业执照
                'user_type' => 2,//角色
                'user_password' => md5(input('user_password')),
                'user_image' => $params['user_image'],
                'user_sheng' => $params['b_sheng'],
                'user_shi' => $params['b_shi'],
                'user_qu' => $params['b_qu'],
            ];
            $res = Db::table('up_u_user')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $areaModel = new AreaModel();
        $province = $areaModel->getProvince();
        $this->assign('province', $province);
        return $this->fetch('broker/brokeradd');
    }
    
    /**
     * @return mixed|\think\response\Json
     * 编辑经纪人
     */
    public function brokerEdit(){
        if(Request::instance()->isPost()){
            $params = input('param.');
    
            $user_name = trim($params['user_name'], ' ');
            $user_name_len = mb_strlen($user_name, 'UTF8');
            if($user_name_len > 15){
                return json(['code' => 100, 'msg' => '用户名不超过15个字符']);
            }
    
            $user_shop = trim($params['user_shop'], ' ');
            $user_shop_len = mb_strlen($user_shop, 'UTF8');
            if($user_shop_len > 100){
                return json(['code' => 100, 'msg' => '门店信息不超过100个字符']);
            }
            $have = Db::table('up_u_user')
                ->where('user_id','<>',$params['user_id'])
                ->where('user_phone',$params['user_phone'])
                ->find();
            if($have){
                return json(['code' => 100, 'msg' => '已有此手机号码！']);
            }
            $update = [
                'user_username' => $user_name,//用户名
                'user_shop' => $user_shop,//门店信息
                'user_phone' => $params['user_phone'],//联系方式
                'user_update_time' => date('Y-m-d H:i:s'),//编辑时间
                'user_sheng' => $params['b_sheng'],
                'user_shi' => $params['b_shi'],
                'user_qu' => $params['b_qu'],
            ];
            //头像
            if(!empty($params['user_image'])){
                $update['user_image'] = $params['user_image'];
            }
            //身份证正面
            if(!empty($params['user_zheng'])){
                $update['user_zheng'] = $params['user_zheng'];
            }
            //身份证反面
            if(!empty($params['user_fan'])){
                $update['user_fan'] = $params['user_fan'];
            }
            //名片
            if(!empty($params['user_card'])){
                $update['user_card'] = $params['user_card'];
            }
            //营业执照
            if(!empty($params['user_zhizhao'])){
                $update['user_zhizhao'] = $params['user_zhizhao'];
            }
            $user = Db::name('u_user')->where('user_id',$params['user_id'])
                ->field('user_type,user_status')->find();
            if($user['user_type'] == 3){
                $update['user_shop'] = Db::table('up_company')->where('id',$user_shop)->value('name');
                $update['user_company_id_3'] = $user_shop;
            }
            $res = Db::table('up_u_user')->where('user_id', $params['user_id'])->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $user_id = input('id');
        $info = Db::table('up_u_user')
            ->where('user_id', $user_id)
            ->field('user_id, user_type, user_company_id_3, user_username, user_shop, user_phone, user_zheng, user_fan,
            user_card, user_zhizhao, user_image, user_sheng, user_shi, user_qu')
            ->find();
        $this->assign('info', $info);
        $coms = Db::name('company')->field('id,name')->select();
        $this->assign('coms', $coms);
        $areaModel = new AreaModel();
        $province = $areaModel->getProvince();
        $this->assign('province', $province);
        // 市
        $city = Db::table('up_area')
            ->where('level', 2)
            ->where('pid', $info['user_sheng'])
            ->field('district_id, district')
            ->select();
        // 区
        $district = Db::table('up_area')
            ->where('level', 3)
            ->where('pid', $info['user_shi'])
            ->field('district_id, district')
            ->select();
        $this->assign([
            'city' => $city,
            'district' => $district
        ]);
        return $this->fetch('broker/brokeredit');
    }
    
    /**
     * @return \think\response\Json
     * 删除经纪人
     */
    public function brokerDel(){
        $user_id = input('id');
        $res = Db::table('up_u_user')->where('user_id', $user_id)->setField('user_del', 2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 评价列表
     * @return mixed|\think\response\Json
     */
    public function brokerComment(){
        if(Request::instance()->isAjax()){
            $user_name = input('user_name');
            $where['uc_user_id'] = session('userid');
            //用户名||手机号
            if(isset($user_name) && $user_name != ''){
                $where['user_username'] = ['like', '%' . $user_name . '%'];
            }
        
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('user_comment')
                ->join('up_u_user', 'user_id = uc_who_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::name('user_comment')
                ->join('up_u_user', 'user_id = uc_who_id', 'left')
                ->field('uc_id, user_username, uc_star, uc_signs, uc_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('uc_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $user_id = input('user_id');
        if($user_id){
            session('userid', $user_id);
        }
        return $this->fetch('broker/brokercomment');
    }
}